<?php
include("dbconnect.php");

//sleep( 3 );
// no term passed - just exit early with no response
if (empty($_GET['term'])) exit ;
//get the passed term and convert to lower case
$q = strtolower($_GET["term"]);
// remove slashes if they were magically added
if (get_magic_quotes_gpc()) $q = stripslashes($q);

//pull an array
$items = array();

$q1 = "SELECT * FROM tags";
$result = mysql_query($q1) or die('Query failed: ' . mysql_error());
while($l = mysql_fetch_array($result, MYSQL_ASSOC)) {
	//echo $l[type].$l[tag];
	$items[] = $l[type].$l[tag];
}
//print_r($items);

//create result array
$result = array();
//check each member of items array
foreach ($items as $value) {
	//echo $value;
	//echo $q;
	//if the $value matches $q
	if (strpos(strtolower($value), $q) !== false) {
		//put the value in the result array
		//echo $value;
		array_push($result, $value);
	}
	//stop giveing options at 5
	if (count($result) > 5)
		break;
}

// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
echo json_encode($result);

?>